package com.ruoyi.web.controller.news;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.system.domain.news.NewsExpertSection;
import com.ruoyi.system.domain.news.dto.NewsExpertSectionDto;
import com.ruoyi.system.domain.news.query.NewsExpertSectionQuery;
import com.ruoyi.system.domain.news.vo.NewsExpertSectionVo;
import com.ruoyi.system.service.news.NewsExpertSectionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/news/newsExpertSection")
@Api(value = "newsExpertSectionController", tags  = "专家专栏")
public class NewsExpertSectionController extends BaseController {

	@Resource
	private NewsExpertSectionService newsExpertSectionService;

    @ApiOperation(value = "新增/修改专家专栏信息", notes = "新增/修改专家专栏信息")
	@PostMapping("/submit")
	public R submit(@RequestBody NewsExpertSectionDto newsExpertSectionDto) {
		LoginUser loginUser = SecurityUtils.getLoginUser();
		NewsExpertSection newsExpertSection = new NewsExpertSection();
		BeanUtils.copyProperties(newsExpertSectionDto,newsExpertSection);
		if (newsExpertSection.getId()==null) {
			newsExpertSection.setCreateBy(loginUser.getUsername());
			newsExpertSection.setDeptId(loginUser.getDeptId());
			newsExpertSection.setCreateTime(new Date());
			newsExpertSection.setUpdateBy(loginUser.getUsername());
			newsExpertSection.setUpdateTime(new Date());
		} else {
			newsExpertSection.setUpdateBy(loginUser.getUsername());
			newsExpertSection.setUpdateTime(new Date());
		}
		boolean result = newsExpertSectionService.saveOrUpdate(newsExpertSection);
		if (result) {
			return R.ok();
		} else {
			return R.fail("操作失败");
		}
	}

	@ApiOperation(value = "删除专家专栏信息", notes = "删除专家专栏信息")
	@DeleteMapping("/delete")
	public R delete(@RequestParam(value = "id") Long id) {
		boolean result = newsExpertSectionService.removeById(id);
		if (result) {
			return R.ok();
		} else {
			return R.fail("操作失败");
		}
	}

	@ApiOperation(value = "获取单个专家专栏信息", notes = "获取单个专家专栏信息")
	@GetMapping("/getById")
	public R<NewsExpertSectionVo> getById(@RequestParam(value = "id") Long id) {
		NewsExpertSectionQuery newsExpertSectionQuery = new NewsExpertSectionQuery();
		newsExpertSectionQuery.setId(id);
		List<NewsExpertSectionVo> newsExpertSectionList = newsExpertSectionService.getNewsExpertSectionList(newsExpertSectionQuery);
		if (newsExpertSectionList!=null&&newsExpertSectionList.size()>0) {
			return R.ok(newsExpertSectionList.get(0));
		} else {
			return R.fail("数据不存在");
		}
	}

	@ApiOperation(value = "获取专家专栏信息列表")
	@PostMapping(value = "/list")
	public R<PageInfo<NewsExpertSectionVo>> list(@RequestBody NewsExpertSectionQuery newsExpertSectionQuery) {
		List<NewsExpertSectionVo> newsExpertSectionList = newsExpertSectionService.getNewsExpertSectionList(newsExpertSectionQuery);
		return R.ok(new PageInfo<>(newsExpertSectionList));
	}
}
